package cn.maydaymemory.maydaycore.configuration;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;

public class ConfigurationManager{
    private String configName;
    private JavaPlugin plugin;
    private YamlConfiguration config;
    private File configPath;

    public ConfigurationManager(JavaPlugin plugin,String config_name){
        this.configName=config_name;
        this.plugin=plugin;
        configPath=new File(plugin.getDataFolder(), config_name);
    }

    public ConfigurationManager reload(){
        if (!configPath.exists()) {
            plugin.saveResource(configName, true);
        }
        config = YamlConfiguration.loadConfiguration(configPath);
        return this;
    }

    public ConfigurationManager save(){
        try {
            config.save(configPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return this;
    }

    public FileConfiguration get() {
        if(config==null) reload();
        return config;
    }
}
